# +_+ coding:utf-8 +_+

#use 5min kline
#ma5 ma10
#ma5 < ma10, buy duo ,ping kong
#ma5 < ma10, buy kong, ping duo
import jury
import kline_pool
from api import *
import time



#ma5>ma10
class FiveMin_MA5_MA10_Jury(jury.Jury):
    _rate = 0
    _status_up = False
    _status_down = False
    def __init__(self):
        super(FiveMin_MA5_MA10_Jury, self).__init__()
        self._rate = api.usd2cny.query()
        self._status_up = False
        self._status_down = False

    def judge(self):
        ma5 = kline_pool.pool.ma5()
        ma10 = kline_pool.pool.ma10()
        klines = kline_pool.pool.getklines()
        #print(klines[-1].to_string())
        #print('-1 ma5val:%f   ma10:%f' %(ma5[-1]*self._rate,ma10[-1]*self._rate))
        #print(klines[-2].to_string())
        #print('-2 ma5val:%f   ma10:%f' %(ma5[-2]*self._rate,ma10[-2]*self._rate))
        #ma5 down then ma10,judge is latest point ma5<ma10 && the -2 ma ma5>ma10
        #r = api.API.usd2cnyrate()
        #t = api.API.ticker()
        #print('trade: ping duo buy kong, now time is %s latestklineis:%s last priceis:%f  buy1:%f  sell1:%f' %(time.localtime(),klines[-1].ts,t.last*r,t.buy*r, t.sell*r))
        #print('trade: ping kong buy duo, now time is %s latestklineis:%s last priceis:%f  buy1:%f  sell1:%f' %(time.localtime(),klines[-1].ts,t.last*r,t.buy*r, t.sell*r))
        #print('trade: ma5-1:%f ma10-1:%f ma5-2:%f ma10-2:%f ping kong buy duo, now time is %s latestklineis:%s last priceis:%f  buy1:%f  sell1:%f' %(ma5[-1],ma10[-1],ma5[-2],ma10[-2],time.localtime(),klines[-1].ts,t.last*r,t.buy*r, t.sell*r))

        if ma10[-1] - ma5[-1] > 0.1:
            if ma5[-2]-ma10[-2] > 0.1:
                #ping duo, buy kong
                if self._status_down:
                    return (0, 0, 0, 0)
                r = api.API.usd2cnyrate()
                t = api.API.ticker()
                print('trade: ma5-1:%f ma10-1:%f ma5-2:%f ma10-2:%f ping duo buy kong, now time is %s latestklineis:%s last priceis:%f  buy1:%f  sell1:%f' %(ma5[-1],ma10[-1],ma5[-2],ma10[-2],time.localtime(),klines[-1].ts,t.last*r,t.buy*r, t.sell*r))
                fd = open('tr.dat','a')
                fd.write('\r\ntrade: ma5-1:%f ma10-1:%f ma5-2:%f ma10-2:%f ping duo buy kong, now time is %s latestklineis:%s last priceis:%f  buy1:%f  sell1:%f' %(ma5[-1],ma10[-1],ma5[-2],ma10[-2],time.localtime(),klines[-1].ts,t.last*r,t.buy*r, t.sell*r))
                fd.close()    
                self._status_down = True
                self._status_up = False
                return (-1,1,19000,10)
                #return(1,-1,19000,10)
        #ma5 up then ma10, judge is latest point ma5>ma10 && the -2 ma ma5<ma10         
        elif ma5[-1]-ma10[-1] > 0.1:   
            if ma10[-2] - ma5[-2] >0.1:
                #ping kong, buy duo
                if self._status_up:
                    return (0, 0, 0, 0)    
                r = api.API.usd2cnyrate()
                t = api.API.ticker()   
                self._status_up = True
                self._status_down = False  
                print('trade: ma5-1:%f ma10-1:%f ma5-2:%f ma10-2:%f ping kong buy duo, now time is %s latestklineis:%s last priceis:%f  buy1:%f  sell1:%f' %(ma5[-1],ma10[-1],ma5[-2],ma10[-2],time.localtime(),klines[-1].ts,t.last*r,t.buy*r, t.sell*r))
                fd = open('tr.dat','a')
                fd.write('\r\ntrade: ma5-1:%f ma10-1:%f ma5-2:%f ma10-2:%f ping kong buy duo, now time is %s latestklineis:%s last priceis:%f  buy1:%f  sell1:%f' %(ma5[-1],ma10[-1],ma5[-2],ma10[-2],time.localtime(),klines[-1].ts,t.last*r,t.buy*r, t.sell*r))
                fd.close()   
                return (-1,-1,10000,10)
                #return (1,1,190000,10)
        else:
            print('no change condition, ma5[-1]:%f  ma10[-1]:%f ma5[-2]:%f ma10[-2]:%f' %(ma5[-1], ma10[-1], ma5[-2], ma10[-2]))
            print(klines[-1].to_string())
            print(klines[-2].to_string())
            
            
        return (0, 0, 0, 0)